package de.unimannheim.ki.algorithmn;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.unimannheim.ki.databaseentities.MusicObject;
import de.unimannheim.ki.databaseentities.Musicuser;

public class LikeHammingJoint implements IComparator {
	@Override
	public List<Neighbour> computeDistance(List<Musicuser> users,
			Musicuser compareuser) {
		double overallrelationrate = 0.0;
		ArrayList<Neighbour> neighbours = new ArrayList<Neighbour>();

		List<MusicObject> myLikedObjects = compareuser.getObjects();
		//loop overall users
		for (int i = 0; i < users.size(); i++) {

			double likeQuota = 0.0;

			Musicuser mu = users.get(i);
			// if same user continue
			
			if (mu.getFbid().equals(compareuser.getFbid()))
				continue;
			
			List<MusicObject> muLikes = mu.getObjects();
			
			int equalcount = 0;
			int count = 0;
			int hemmingdistance = 0;
			for (MusicObject myObject : myLikedObjects) {
				 for (MusicObject musicObject : muLikes) {
					 hemmingdistance = this.getHammingDistance(myObject.getName(), musicObject.getName());
					if(hemmingdistance != -1 && hemmingdistance < 10){
						equalcount++;
					}//if
					count++;
				}//for
			}//for
			
			if(myLikedObjects.size()==0) continue;
			
			if(count == 0) {
				 overallrelationrate =  0.0;
			} else {
				 overallrelationrate =  ((double)((double) equalcount) / ((double) count));
			}
		
			 neighbours.add(new Neighbour(overallrelationrate, mu));
		}// loop over users
		Collections.sort(neighbours);

	return neighbours;
	}
	
	private int getHammingDistance(String compOne, String compTwo){
        if (compOne.length() != compTwo.length()){
            return -1;
        }
        int counter = 0;
        for (int i = 0; i < compOne.length(); i++) {
            if (compOne.charAt(i) != compTwo.charAt(i)) counter++;
        }
        return counter;
    }

}
